#!/bin/sh
# PCP QA Test No. 1977
# Exercise pcp2opentelemetry HTTP POST functionality.
#
# Copyright (c) 2025 Red Hat.  All Rights Reserved.
#


seq=`basename $0`
echo "QA output created by $seq"

. ./common.python

which pcp2opentelemetry >/dev/null 2>&1 || _notrun "pcp2opentelemetry not installed"


_cleanup()
{
    cd $here
    _restore_config $PCP_SYSCONF_DIR/labels
    $sudo rm -rf $tmp $tmp.*
}

status=0    # success is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_pcp2opentelemetry_http()
{
   tee -a $here/$seq.full \
   | col -b \
   | sed \
       -e '/\"asInt\":/ s/[0-9][0-9]*/INTEGER/' \
       -e '/\"asDouble\":/ s/[0-9][0-9]*/DOUBLE/' \
       -e 's/\"version\": .*/\"version\": VERSION/' \
       -e 's/\"stringValue\": \".*\"/\"stringValue\": \"STRING\"/' \
       -e 's/\"timeUnixNano\": [0-9][0-9]*\.[0-9][0-9]*.*/\"timeUnixNano\": TIME/' \
       -e '/HTTP/d'
} >> $tmp.python2.out


# real QA test starts here
_save_config $PCP_SYSCONF_DIR/labels
$sudo rm -rf $PCP_SYSCONF_DIR/labels/*

port=`_find_free_port`
$PCP_PYTHON_PROG $here/src/pythonserver.py $port >$tmp.python.out 2>&1 &
pid=$!
sleep 2 # let server start up


echo "pcp2opentelemetry invocation" | tee -a $here/$seq.full
pcp2opentelemetry -s1 -u http://localhost:$port/receive hinv.ncpu >$tmp.json.out 2>$tmp.openjson.err


echo "pcp2opentelemetry HTTP POST (sorted):"
_filter_pcp2opentelemetry_http <$tmp.python.out
_check_http_filter <$tmp.python2.out


# terminate pythonserver.py now
pmsignal $pid >/dev/null 2>&1

# success, all done
exit
